<?php
class ContractService extends AbstractService {
    /**
     * @var ContractService
     */
    private static $instance;

    /**
     * Get the instance of ContractService.
     *
     * @return ContractService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new ContractService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct() {
        parent::__construct();
    }

    /**
     * 处理用户创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId) {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId) {
        // do nothing
    }

    /**
     * 处理 service 对外提供的全量数据
     * @param int $userId
     * @return mixed|void
     */
    public function info($userId) {

    }

    /**
     * 获取合约系统数据&刷新
     * @param $userId
     * @param $session
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getContractInfo($userId, $session, $reqTime) {
        //平台活动时间区分
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];

        //获取合约系统的相关配置
        $contractConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $session, true);
        $contractTaskConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_CONTRACT_TASK, null, true);
        $contractRewardConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_CONTRACT_REWARD, null, true);
        if (!$contractConf || empty($contractConf) ||
            !$contractTaskConf || empty($contractTaskConf) ||
            !$contractRewardConf || empty($contractRewardConf)){
            return array(
                'code' => 1,
                'msg' => '缺少配置',
                'data' => array(
                    'contract' => array(),
                    'task' => array(),
                    'reward' => array()
                )
            );
        }

        //验证合约期数是否正确
        if ($platform != 'dev'){
            $contractStart = strtotime($contractConf[$platformPre.'TriggerTime']);
            $contractEnd = strtotime($contractConf[$platformPre.'EndTime']);
        }
        else{
            $contractStart = $contractConf['startTime'];
            $contractEnd = $contractConf['endTime'];
//test0813
            $contractStart = strtotime($contractConf['weixinTriggerTime']);
            $contractEnd = strtotime($contractConf['weixinEndTime']);
        }

        if ($reqTime < $contractStart || $reqTime > $contractEnd){
            return array(
                'code' => 2,
                'msg' => '合约期数错误',
                'data' => array(
                    'contract' => array(),
                    'task' => array(),
                    'reward' => array()
                )
            );
        }

        //获取玩家合约系统结构数据
        /**
         * @var $contractDataList ContractDataVoList
         * @var $contractData ContractDataVo
         */
        $contractDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_DATA);
        $contractData = $contractDataList->getElement($session,false);
        if (!$contractData){
            //数据创建
            $contractData = new ContractDataVo(
                $userId,
                $session,
                0,
                0,
                0,
                $reqTime,
                $reqTime,
                $contractEnd,
                true
            );

            if ($contractData->isInsert() && !SzUtility::checkArrayKey($session, $contractDataList->getList())) {
                $contractDataList->addElement($contractData);
                $contractDataList->setElement($session,$contractData);
            }
        }
        SzPersister::get()->setVoList($contractDataList);

        //获取玩家合约系统的任务数据
        /**
         * @var $contractTaskList ContractTaskVoList
         * @var $contractTask ContractTaskVo
         */
        $contractTaskList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_TASK);
        $num = 3;

        //获取昨日玩家的合约任务缓存
        $yesTodayTask = array();
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        for ($t=1 ; $t<=$num; $t++){
            $yesTodayTaskId = $redisDb->get(GameConst::CACHE_KEY_CONTRACT_TASK.'-'.$userId.'-'.$t.':');
            if ($yesTodayTaskId && $yesTodayTaskId != 0){
                $yesTodayTask[] = $yesTodayTaskId;
            }
        }

        $todayStart = strtotime(date('Ymd',$reqTime));
        $toNight = $todayStart + 86399;

        $canRandTask1 = array();
        $canRandTask2 = array();
        foreach ($contractTaskConf as $tid => $taskConf){
            if ($taskConf['shield'] != 'dev'){
                if ($taskConf['shield'] == GameConst::SESSION_CONTRACT_PLATFORM_TASK_ARRAY[$platform]){
                    continue;
                }
            }

            if ($taskConf['library'] == 1 && !in_array($tid,$yesTodayTask)){
                $canRandTask1[] = $taskConf;
            }
            if ($taskConf['library'] == 2 && !in_array($tid,$yesTodayTask)){
                $canRandTask2[] = $taskConf;
            }
        }

        $temp = array();
        $point = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'contractTaskPoint', true);
        if ($contractTaskList->getListCount() != 3){
            for ($i=1 ; $i <= $num ; $i++){
                $contractTask = $contractTaskList->getElement($i,false);
                if (!$contractTask){
                    //随机任务
                    $temp = $this->randTask($canRandTask1,$canRandTask2,$i);
                    $getTask = $temp['task'];

                    //数据创建
                    $contractTask = new ContractTaskVo(
                        $userId,
                        $i,
                        $getTask['taskType'],
                        $getTask['taskId'],
                        $getTask['randNum'],
                        0,
                        0,//0:未开始做任务1:正在做任务2:任务已经完成
                        $point['value'],
                        $reqTime,
                        $reqTime,
                        $toNight,
                        true
                    );

                    if ($contractTask->isInsert() && !SzUtility::checkArrayKey($i, $contractTaskList->getList())) {
                        $contractTaskList->addElement($contractTask);
                        $contractTaskList->setElement($i,$contractTask);
                    }
                    if ($i <= 2){
                        unset($canRandTask1[$temp['has']]);
                    }
                    else{
                        unset($canRandTask2[$temp['has']]);
                    }
                    $redisDb->set(GameConst::CACHE_KEY_CONTRACT_TASK.'-'.$userId.'-'.$i.':',$getTask['taskId']);
                }
            }
        }
        else{
            //任务刷新
            foreach ($contractTaskList->toArray() as $pos => $data){
                if ($data['updateTime'] <= $todayStart || $data['updateTime'] >= $toNight){
                    //随机任务
                    $temp = $this->randTask($canRandTask1,$canRandTask2,$pos);
                    $getTask = $temp['task'];

                    $contractTask = $contractTaskList->getElement($pos,false);
                    $contractTask->setType($getTask['taskType']);
                    $contractTask->setTaskId($getTask['taskId']);
                    $contractTask->setNum($getTask['randNum']);
                    $contractTask->setHasNum(0);
                    $contractTask->setState(0);
                    $contractTask->setPoint($point['value']);
                    $contractTask->setUpdateTime($reqTime);
                    $contractTask->setEndTime($toNight);
                    $contractTaskList->updateElement($pos,$contractTask);

                    if ($pos <= 2){
                        unset($canRandTask1[$temp['has']]);
                    }
                    else{
                        unset($canRandTask2[$temp['has']]);
                    }
                    $redisDb->set(GameConst::CACHE_KEY_CONTRACT_TASK.'-'.$userId.'-'.$pos.':',$getTask['taskId']);
                }
                else{
                    $redisDb->set(GameConst::CACHE_KEY_CONTRACT_TASK.'-'.$userId.'-'.$pos.':',$data['taskId']);
                }
            }
        }
        SzPersister::get()->setVoList($contractTaskList);

        //获取玩家合约系统的领奖数据
        /**
         * @var $contractRewardList ContractRewardVoList
         * @var $contractReward ContractRewardVo
         */
        $contractRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_REWARD);
        $contractReward = $contractRewardList->getElement($session,false);
        $seniorSituation = $generalSituation = $this->makeFlag($contractRewardConf,3,$session);
        if (!$contractReward){
            //数据创建
            $contractReward = new ContractRewardVo(
                $userId,
                $session,
                0,
                json_encode($generalSituation),
                json_encode($seniorSituation),
                $reqTime,
                $reqTime,
                $contractEnd,
                true
            );

            if ($contractReward->isInsert() && !SzUtility::checkArrayKey($session, $contractRewardList->getList())) {
                $contractRewardList->addElement($contractReward);
                $contractRewardList->setElement($session,$contractReward);
            }
        }
        SzPersister::get()->setVoList($contractRewardList);
        //格式转换
        $reward = $contractReward->toArray();
        $reward['generalSituation'] = explode(',',$reward['generalSituation']);
        $reward['seniorSituation'] = explode(',',$reward['seniorSituation']);

        return array(
            'code' => 0,
            'msg' => '成功',
            'data' => array(
                'contract' => $contractData->toArray(),
                'task' => $contractTaskList->toArray(),
                'reward' => $reward
            )
        );
    }

    /**
     * 获取合约奖励
     * @param $userId
     * @param $session
     * @param $level
     * @param $type
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getContractReward($userId, $session, $level, $type, $reqTime) {
        //获取相关配置
        $contractConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, $session, true);
        $contractRewardConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_CONTRACT_REWARD, null, true);
        $rewardConf = array();
        $situation = array();
        foreach ($contractRewardConf as $key=>$data){
            if ($level == $data['contractLevel'] && $session == $data['activityId']){
                $rewardConf = $data;
            }
        }

        if (!$contractConf || empty($contractConf) ||
            !$rewardConf || empty($rewardConf) ||
            !$contractRewardConf || empty($contractRewardConf)){
            return array(
                'code' => 1,
                'msg' => '缺少配置',
                'data' => array(
                    'type' => $type,
                    'situation' => $situation
                )
            );
        }

        //获取玩家合约系统结构数据
        /**
         * @var $contractDataList ContractDataVoList
         * @var $contractData ContractDataVo
         */
        $contractDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_DATA);
        $contractData = $contractDataList->getElement($session,false);
        if (!$contractData){
            return array(
                'code' => 4,
                'msg' => '数据不存在',
                'data' => array(
                    'type' => $type,
                    'situation' => $situation
                )
            );
        }

        //获取玩家当前领奖状态数据
        /**
         * @var $contractRewardList ContractRewardVoList
         * @var $contractReward ContractRewardVo
         */
        $contractRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_REWARD);
        $contractReward = $contractRewardList->getElement($session,false);
        if (!$contractReward){
            return array(
                'code' => 4,
                'msg' => '数据不存在',
                'data' => array(
                    'type' => $type,
                    'situation' => $situation
                )
            );
        }

        $items = array();
        $generalSituation = array();
        $seniorSituation = array();
        //普通领奖处理
        if ($type == 1){
            $generalSituation = explode(',',json_decode($contractReward->getGeneralSituation()));
            if ($generalSituation[$level - 1] == 2 || $generalSituation[$level - 1] == 0){
                return array(
                    'code' => 6,
                    'msg' => '不可领奖',
                    'data' => array(
                        'type' => $type,
                        'situation' => $situation
                    )
                );
            }
            $generalSituation[$level - 1] = 2;
            $items = $rewardConf['commonReward'];
            $situation = $generalSituation;
        }

        //高级领奖处理
        if ($type == 2){
            if ($contractReward->getSenior() == 0){
                return array(
                    'code' => 5,
                    'msg' => '领奖类型错误',
                    'data' => array(
                        'type' => $type,
                        'situation' => $situation
                    )
                );
            }
            $seniorSituation = explode(',',json_decode($contractReward->getSeniorSituation()));
            if ($seniorSituation[$level - 1] == 2 || $seniorSituation[$level - 1] == 0){
                return array(
                    'code' => 6,
                    'msg' => '不可领奖',
                    'data' => array(
                        'type' => $type,
                        'situation' => $situation
                    )
                );
            }
            $seniorSituation[$level - 1] = 2;
            $items = $rewardConf['advancedReward'];
            $situation = $seniorSituation;
        }

        //进行发奖
        foreach ($items as $item=>$count){
            GameService::get()->handleEntity($userId, $item, $count, true);
        }

        if ($type == 1){
            $contractReward->setGeneralSituation(implode(',',$generalSituation));
        }
        if ($type == 2){
            $contractReward->setSeniorSituation(implode(',',$seniorSituation));
        }
        $contractRewardList->updateElement($session,$contractReward);
        SzPersister::get()->setVoList($contractRewardList);

        //获取玩家等级
        $userLevel = UserService::get()->getUserLevel($userId);
        //数据采集
        if ($type == 2) {
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:contract|action:getSenior|',
                    'item' => 'senior&'.$userLevel,
                    'value' => 1
                )
            ));
        }
        if ($type == 1) {
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:contract|action:getGeneral|',
                    'item' => 'general&'.$userLevel,
                    'value' => 1
                )
            ));
        }

        return array(
            'code' => 0,
            'msg' => '成功',
            'data' => array(
                'type' => $type,
                'situation' => $situation
            )
        );
    }


    public function getContractTaskInfo() {

    }

    /**
     * 解锁高级领奖状态
     * @param $userId
     * @param $session
     * @param $reqTime
     * @throws SzException
     */
    public function setSeniorSituation($userId,$session,$reqTime) {
        /**
         * @var $contractDataList ContractDataVoList
         * @var $contractData ContractDataVo
         */
        $contractDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_DATA);
        $contractData = $contractDataList->getElement($session,false);

        /**
         * @var $contractRewardList ContractRewardVoList
         * @var $contractReward ContractRewardVo
         */
        $contractRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_REWARD);
        $contractReward = $contractRewardList->getElement($session,false);

        if (!$contractData || !$contractReward){
            return;
        }

        $isSenior = $contractReward->getSenior();
        if ($isSenior == 1){
            return;
        }

        //获取玩家合约等级
        $level = $contractData->getLevel();
        //更新领奖状态
        $contractReward->setSenior(1);
        //高级奖励状态解锁
        $seniorSituation = explode(',',json_decode($contractReward->getSeniorSituation()));
        foreach ($seniorSituation as $key=>$state){
            if ($key <= $level - 1){
                $seniorSituation[$key] = 1;
            }
            else{
                break;
            }
        }
        $contractReward->setSeniorSituation(implode(',',$seniorSituation));
        $contractReward->setUpdateTime($reqTime);
        if ($_SERVER['is_payment_callback'] ?? false) $contractReward->persist();
        $contractRewardList->updateElement($session,$contractReward);
        SzPersister::get()->setVoList($contractRewardList);

        return;
    }

    /**
     * 更新进度，奖励状态，合约数据
     * @param $userId
     * @param $session
     * @param $pos
     * @param $taskId
     * @param $updateTime
     * @param int $process
     * @throws SzException
     */
    public function updateTaskProgress($userId,$session,$pos,$taskId,$updateTime,$process=1) {
        //获取玩家任务数据
        /**
         * @var $contractTaskList ContractTaskVoList
         * @var $contractTask ContractTaskVo
         */
        $contractTaskList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_TASK);
        $contractTask = $contractTaskList->getElement($pos,false);

        /**
         * @var $contractDataList ContractDataVoList
         * @var $contractData ContractDataVo
         */
        $contractDataList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_DATA);
        $contractData = $contractDataList->getElement($session,false);

        /**
         * @var $contractRewardList ContractRewardVoList
         * @var $contractReward ContractRewardVo
         */
        $contractRewardList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_REWARD);
        $contractReward = $contractRewardList->getElement($session,false);

        if (!$contractTask || !$contractData || !$contractReward){
            return;
        }

        if ($contractTask->getState() == 2){
            return;
        }

        //进行数据更新
        $nowProcess = $contractTask->getHasNum();
        $totalProcess = $contractTask->getNum();
        $canAddPoint = false;
        if ($nowProcess + $process >= $totalProcess){
            $contractTask->setState(2);
            $canAddPoint = true;
        }else{
            $contractTask->setState(1);
        }
        $contractTask->setUpdateTime($updateTime);
        $contractTask->setHasNum($nowProcess + $process);
        $contractTaskList->updateElement($pos,$contractTask);
        SzPersister::get()->setVoList($contractTaskList);


        if ($canAddPoint){
            //获取公用配置
            $addPoint = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'contractTaskPoint', true);
            $needPoint = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'contractLevelup', true);

            //积分添加
            $hasPoint = $contractData->getNowPoint();
            $totalPoint = $contractData->getPoint();
            $nowLevel = $newLevel  = $contractData->getLevel();
            if ($nowLevel < 15){
                if ($hasPoint + $addPoint['value'] >= $needPoint['value']){
                    $contractData->setNowPoint($hasPoint + $addPoint['value'] - $needPoint['value']);
                    $contractData->setLevel($nowLevel + 1);
                    $newLevel = $nowLevel + 1;
                }
                else{
                    $contractData->setNowPoint($hasPoint + $addPoint['value']);
                }
                $contractData->setPoint($totalPoint + $addPoint['value']);
            }
            if ($nowLevel == 15){
                if ($hasPoint + $addPoint['value'] < $needPoint['value']){
                    $contractData->setNowPoint($hasPoint + $addPoint['value']);
                    $contractData->setPoint($totalPoint + $addPoint['value']);
                }
                else{
                    $contractData->setNowPoint($needPoint['value']);
                    $contractData->setPoint($nowLevel*$needPoint['value']);
                }
            }

            $contractData->setUpdateTime($updateTime);
            $contractDataList->updateElement($session,$contractData);
            SzPersister::get()->setVoList($contractDataList);

            //领奖状态修改
            if ($newLevel == $nowLevel + 1){
                $isSenior = $contractReward->getSenior();
                if ($isSenior){
                    //高级奖励状态解锁
                    $seniorSituation = explode(',',json_decode($contractReward->getSeniorSituation()));
                    $seniorSituation[$newLevel - 1] = 1;
                    $contractReward->setSeniorSituation(implode(',',$seniorSituation));
                }

                //普通奖励状态解锁
                $generalSituation = explode(',',json_decode($contractReward->getGeneralSituation()));
                $generalSituation[$newLevel - 1] = 1;
                $contractReward->setGeneralSituation(implode(',',$generalSituation));
                $contractReward->setUpdateTime($updateTime);

                $contractRewardList->updateElement($session,$contractReward);
                SzPersister::get()->setVoList($contractRewardList);
            }
        }

        return;
    }

    /**
     * 任务随机生成
     * @param $canRandTask1
     * @param $canRandTask2
     * @param $pos
     * @return array
     */
    public function randTask($canRandTask1,$canRandTask2,$pos) {
        $numLevel = 1;
        $conf = $canRandTask1;
        //随机任务ID
        if ($pos > 2){
            $conf = $canRandTask2;
        }

        $taskKey = array_rand($conf);
        $numLevel = array_rand($conf[$taskKey]['randNum']);
        $getTask = $conf[$taskKey];
        $getTask['randNum'] = $getTask['randNum'][$numLevel];

        return array(
            'task' => $getTask,
            'has' => $taskKey
        );
    }

    /**
     * 字符串生成
     * @param $conf
     * @param $type
     * @param $session
     * @return string
     */
    public function makeFlag($conf,$type,$session) {
        $str = '';
        $i=1;
        $useConf = array();
        foreach ($conf as $tempKey=>$temp){
            if ($temp['activityId'] == $session){
                $useConf[] = $temp;
            }
        }

        $times = count($useConf);
        if ($type == 3){//制作初始标记字符串
            foreach ($useConf as $key=>$data){
                if ($i != $times){
                    $str .= '0,';
                }
                else{
                    $str .= '0';
                }
                $i++;
            }
        }

        return $str;
    }

    /**
     * 获取正在开启的合约以及任务数据
     * @param $userId
     * @param $taskType
     * @param int $nowTime
     * @return array
     * @throws SzException
     */
    public function getContractOpenAndTask($userId,$taskType,$nowTime=0) {
        //平台活动时间区分
        $platform = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_USER_PLATFORM);
        $platformPre = GameConst::PLATFORM_TIME_ARRAY[$platform];
        //获取合约系统的相关配置
        $contractConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GAME_EVENT, null, true);
        //当前时间获取
        if (!$nowTime){
            $nowTime = SzSystem::getSysTime();
        }

        //当前时间所在日期的凌晨
        $todayStartTime = strtotime(date('Ymd',$nowTime));

        $useConf = array();
        $contractStart = 0;
        $contractEnd = 0;

        foreach ($contractConf as $aid=>$conf){
            if ($conf['functionId'] == GameConst::FUNC_CONTRACT_FUNC){
                //验证合约期数是否正确
                if ($platform != 'dev'){
                    $contractStart = strtotime($conf[$platformPre.'TriggerTime']);
                    $contractEnd = strtotime($conf[$platformPre.'EndTime']);
                }
                else{
                    $contractStart = strtotime($conf['weixinTriggerTime']);
                    $contractEnd = strtotime($conf['weixinEndTime']);
                }
                if ($nowTime >= $contractStart && $nowTime <= $contractEnd){
                    $useConf = $conf;
                    break;
                }
            }
        }

        $taskData = array();
        if (empty($useConf)){
            return array();
        }
        else{
            //获取玩家任务数据
            /**
             * @var $contractTaskList ContractTaskVoList
             * @var $contractTask ContractTaskVo
             */
            $contractTaskList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_CONTRACT_TASK);
            foreach ($contractTaskList->toArray() as $key=>$data){
                if ($data['type'] == $taskType){
                    $taskData = $data;
                    break;
                }
            }

            if (!empty($taskData) && $taskData['updateTime'] < $todayStartTime){
                $point = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'contractTaskPoint', true);
                $toNight = $todayStartTime + 86399;
                //进行所有任务初始化
                $contractTaskConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_CONTRACT_TASK, null, true);

                //获取昨日玩家的合约任务缓存
                $yesTodayTask = array();
                $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
                $num = 3;
                for ($t=1 ; $t<=$num; $t++){
                    $yesTodayTaskId = $redisDb->get(GameConst::CACHE_KEY_CONTRACT_TASK.'-'.$userId.'-'.$t.':');
                    if ($yesTodayTaskId && $yesTodayTaskId != 0){
                        $yesTodayTask[] = $yesTodayTaskId;
                    }
                }

                $canRandTask1 = array();
                $canRandTask2 = array();
                foreach ($contractTaskConf as $tid => $taskConf){
                    if ($taskConf['shield'] != 'dev'){
                        if ($taskConf['shield'] == GameConst::SESSION_CONTRACT_PLATFORM_TASK_ARRAY[$platform]){
                            continue;
                        }
                    }

                    if ($taskConf['library'] == 1 && !in_array($tid,$yesTodayTask)){
                        $canRandTask1[] = $taskConf;
                    }
                    if ($taskConf['library'] == 2 && !in_array($tid,$yesTodayTask)){
                        $canRandTask2[] = $taskConf;
                    }
                }

                foreach ($contractTaskList->toArray() as $pos => $data){
                    //随机任务
                    $temp = $this->randTask($canRandTask1,$canRandTask2,$pos);
                    $getTask = $temp['task'];

                    $contractTask = $contractTaskList->getElement($pos,false);
                    $contractTask->setType($getTask['taskType']);
                    $contractTask->setTaskId($getTask['taskId']);
                    $contractTask->setNum($getTask['randNum']);
                    $contractTask->setHasNum(0);
                    $contractTask->setState(0);
                    $contractTask->setPoint($point['value']);
                    $contractTask->setUpdateTime($nowTime);
                    $contractTask->setEndTime($toNight);
                    $contractTaskList->updateElement($pos,$contractTask);

                    if ($pos <= 2){
                        unset($canRandTask1[$temp['has']]);
                    }
                    else{
                        unset($canRandTask2[$temp['has']]);
                    }
                    $redisDb->set(GameConst::CACHE_KEY_CONTRACT_TASK.'-'.$userId.'-'.$pos.':',$getTask['taskId']);
                }
                SzPersister::get()->setVoList($contractTaskList);
                return array();
            }

            return array(
                'session' => $useConf['indexId'],
                'start' => $contractStart,
                'end' => $contractEnd,
                'task' => $taskData
            );
        }
    }
}
